package 刷题10月.每日一题0930;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/
public class 树230二叉搜索树中第K小的元素1017 {

     public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }

    class Solution {
        List<Integer> list = new ArrayList<>();
        public int kthSmallest(TreeNode root, int k) {
            dfs(root);
            Collections.sort(list);
            return list.get(k - 1);
        }
        void dfs(TreeNode root) {
            if (root == null) return ;
            list.add(root.val);
            dfs(root.left);
            dfs(root.right);
        }
    }
}
